/*
 * @lc app=leetcode.cn id=589 lang=javascript
 *
 * [589] N叉树的前序遍历
 */

// @lc code=start
/**
 * // Definition for a Node.
 * function Node(val, children) {
 *    this.val = val;
 *    this.children = children;
 * };
 */

/**
 * @param {Node} root
 * @return {number[]}
 */
var preorder = function(root) {
    if(!root)return []
    let arr=[]
    let queue=[root]
    while(queue.length){
        let node=queue.shift()
        arr.push(node.val)
        if(node.children){
            queue=node.children.concat(queue)
        }
    }
    return arr
};//bfs
// @lc code=end

var preorder = function(root) {
    let arr=[]
    let dfs=function(node){
        if(!node)return 
        arr.push(node.val)
        for(let i in node.children){
            dfs(node.children[i])
        }
    }
    dfs(root)
    return arr
};//dfs